/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant.ant.taskdefs;


import java.io.File;
import java.util.Enumeration;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.types.FilterSet;


/**
 * <p>
 * This class is a sub-class of the Copy ant task that
 * is used to un-copy all the files that normally would
 * have been copied
 * </p>
 *
 * <p>
 * This version of the uncopy task only works with ant
 * version 1.6.1
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class UncopyTask extends Copy {


    /**
     * Constructs a new <code>UncopyTask</code>
     */
    public UncopyTask() {
        forceOverwrite = true;
    }


    /**
     * Un-support for un-copy, throws BuildException
     */
    public void setOverwrite(String overwrite) {
        throw new BuildException("overwrite not supported for the un-copy task");
    }

    /**
     * Un-support for un-copy, throws BuildException
     */
    public FilterChain createFilterChain() {
        throw new BuildException("filterchain not supported for the un-copy task");
    }

    /**
     * Un-support for un-copy, throws BuildException
     */
    public FilterSet createFilterSet() {
        throw new BuildException("filterset not supported for the un-copy task");
    }

    /**
     * Overrides the doFileOperations from the Copy task. Rather than doing the
     * copy here, this version deletes the destination file instead. Hence, un-
     * copying the file.
     */
    protected void doFileOperations() {

        if (fileCopyMap.size() > 0) {
            log("Un-copying " + fileCopyMap.size()
                + " file" + (fileCopyMap.size() == 1 ? "" : "s")
                + " to " + destDir.getAbsolutePath());

            Enumeration e = fileCopyMap.keys();
            while (e.hasMoreElements()) {
                String fromFile = (String) e.nextElement();
                String[] toFiles = (String[]) fileCopyMap.get(fromFile);

                for (int i = 0; i < toFiles.length; i++) {
                    if (fromFile.equals(toFiles[i])) {
                        log("Skipping self-un-copy of " + fromFile, Project.MSG_DEBUG);
                        continue;
                    }

                    log("Un-copying " + fromFile + " to " + toFiles[i], Project.MSG_DEBUG);

                    File remove = new File(toFiles[i]);
                    if (remove.exists()) {
                        if (!remove.delete()) {
                            throw new BuildException("Failed to un-copy " +
                                fromFile + " to " + toFiles[i]);
                        }
                    }
                }
            }
        }

        if (includeEmpty) {
            Enumeration e = dirCopyMap.elements();
            int count = 0;
            while (e.hasMoreElements()) {
                String[] dirs = (String[]) e.nextElement();
                for (int i = 0; i < dirs.length; i++) {
                    File dir = new File(dirs[i]);
                    if (dir.exists()) {
                        dir.delete();
                        count++;
                    }
                }
            }

            if (count > 0) {
                log("Un-copied " + count + " empty director" + (count == 1 ? "y" : "ies") +
                    " to " + destDir.getAbsolutePath());
            }
        }
    }
}